//
//  ModelController.swift
//  压岁_IOS
//
//  Created by 梓简王 on 2017/7/12.
//  Copyright © 2017年 梓简王. All rights reserved.
//

import UIKit
import QorumLogs

protocol modelControllerDelegate{
    func clickSignOut()
    func switchAbleTOSide(able:Bool)
}

class ModelController: NSObject, UIPageViewControllerDataSource,dataViewControllerDelegate {

    var pageData: [String] = []
    var pageBackColor: [UIColor] = []
    var pageCenterImage: [UIImage] = []
    var delegate:modelControllerDelegate?
    
    override init() {
        super.init()
        // Create the data model.
        let dateFormatter = NSDateFormatter()
        pageData = ["Papa","Mama","Baby"]
        pageBackColor = [commonVariable.PAPACOLOR,commonVariable.MAMACOLOR,commonVariable.BABYCOLOR]
        pageCenterImage = [UIImage(named: "man")!,UIImage(named: "woman")!,UIImage(named: "baby")!]
    }

    func viewControllerAtIndex(index: Int, storyboard: UIStoryboard) -> DataViewController? {
        // Return the data view controller for the given index.
        if (self.pageData.count == 0) || (index >= self.pageData.count) {
            return nil
        }

        // Create a new view controller and pass suitable data.
        let dataViewController = storyboard.instantiateViewControllerWithIdentifier("DataViewController") as! DataViewController
        dataViewController.delegate = self
        dataViewController.dataObject = self.pageData[index]
        dataViewController.backColor = self.pageBackColor[index]
        dataViewController.centerImage = self.pageCenterImage[index]
        dataViewController.userMasterRole = index
        return dataViewController
    }

    func indexOfViewController(viewController: DataViewController) -> Int {
        // Return the index of the given data view controller.
        // For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
        return pageData.indexOf(viewController.dataObject) ?? NSNotFound
    }

    // MARK: - Page View Controller Data Source

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        var index = self.indexOfViewController(viewController as! DataViewController)
        if (index == 0) || (index == NSNotFound) {
            self.delegate?.clickSignOut()
            return nil
        }
        index--
        return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        var index = self.indexOfViewController(viewController as! DataViewController)
        if index == NSNotFound {
            return nil
        }
        index++
        if index == self.pageData.count {
            return nil
        }
        return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
    }
    //DataView代理事件
    func switchAbleTOSide(able: Bool) {
        self.delegate?.switchAbleTOSide(able)
    }
    func registerAction() {
        self.delegate?.clickSignOut()
    }
}

